package dao;

import java.util.ArrayList;
import java.util.List;

import model.Tarefa;
import model.Usuario;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import db.GerarTabelas;

public class TarefaDao {

	public static void adicionarTarefa(Tarefa tarefa) {

		Session session = GerarTabelas.preparaSessao();
		session.save(tarefa);
		session.beginTransaction().commit();
		session.close();
	}

	public static List<Tarefa> retornarTodasAsTarefasPendente(Long id_usuario) {
		Session session = GerarTabelas.preparaSessao();

		Criteria criteria = session.createCriteria(Tarefa.class);	
		criteria.add(Restrictions.eq("id_usuario", id_usuario));
		List<Tarefa> tarefas = new ArrayList<>();
		tarefas = criteria.list();
		
		List<Tarefa> tarefasPorUsuario = new ArrayList<Tarefa>();
		for(int i = 0; i < tarefas.size(); i++){
			if(tarefas.get(i).isPendente() == true){
				tarefasPorUsuario.add(tarefas.get(i));
			}
		}

		return tarefasPorUsuario;
	}

	public static List<Tarefa> retornarTodasAsTarefasFeitas(Long id_usuario) {

		Session session = GerarTabelas.preparaSessao();

		Criteria criteria = session.createCriteria(Tarefa.class);
		criteria.add(Restrictions.eq("id_usuario", id_usuario));
		List<Tarefa> tarefas = new ArrayList<>();
		tarefas = criteria.list();

		List<Tarefa> tarefasPorUsuario = new ArrayList<Tarefa>();
		for(int i = 0; i < tarefas.size(); i++){
			if(tarefas.get(i).isPendente() == false){
				tarefasPorUsuario.add(tarefas.get(i));
			}
		}
		return tarefasPorUsuario;
	}
	
	public static List<Tarefa> retornarTodasAsTarefasPorUsuario(Long id_usuario) {

		Session session = GerarTabelas.preparaSessao();

		Criteria criteria = session.createCriteria(Tarefa.class);
		criteria.add(Restrictions.eq("id_usuario", id_usuario));
		List<Tarefa> tarefas = new ArrayList<>();
		tarefas = criteria.list();
		return tarefas;
	}

	public static void deletarTarefa(Tarefa tarefa) {
		Session session = GerarTabelas.preparaSessao();
		Transaction transaction = session.beginTransaction();
		Tarefa tarefaBd = (Tarefa) session.load(Tarefa.class, tarefa.getId());
		session.delete(tarefaBd);
		transaction.commit();
		session.close();

	}

	public static Tarefa buscarTarefa(String tarefa) {
		Session session = GerarTabelas.preparaSessao();
		Transaction transaction = session.beginTransaction();
		Criteria criteria = session.createCriteria(Tarefa.class);
		criteria.add(Restrictions.eq("nomeTarefa", tarefa));
		Tarefa tar = (Tarefa) criteria.uniqueResult();

		return tar;

	}
	
	public static Tarefa buscarTarefaPorId(Long tarefas) {
		Session session = GerarTabelas.preparaSessao();
		Transaction transaction = session.beginTransaction();
		Criteria criteria = session.createCriteria(Tarefa.class);
		criteria.add(Restrictions.eq("id", tarefas));
		Tarefa tar = (Tarefa) criteria.uniqueResult();

		return tar;

	}

	public static void modificarTarefa(Tarefa tarefa) {
		Session session = GerarTabelas.preparaSessao();
		Transaction transaction = session.beginTransaction();
		Tarefa tarefaBd = (Tarefa) session.load(Tarefa.class, tarefa.getId());
		tarefaBd = tarefa;
		session.update(tarefaBd);
		transaction.commit();
		session.close();

	}
}
